package edu.bnu.likun;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

//import sun.org.mozilla.javascript.internal.json.JsonParser;

/**
 * ����ѧ���Ų�ѯ���г���Ϣ������Ϣ
 */
@WebServlet(name="queryCarPay",urlPatterns="/getcarspay")
public class QueryCarPay extends HttpServlet {
	private static final long serialVersionUID = 1L;
		
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryCarPay() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		request.setCharacterEncoding("gbk");
//		response.setContentType("application/json;charSet=GBK");text/plain
		String driverName = getServletContext().getInitParameter("driver");
		String dbURL = getServletContext().getInitParameter("url"); //1433�����ݿ�Ķ˿ڣ�"li"��������ݿ�����
		String userName = getServletContext().getInitParameter("username"); //sa�����ݿ�ĳ����û�����ò�Ҫ��������֣�Ȩ������
		String userPwd = getServletContext().getInitParameter("password"); //sa������  
		response.setContentType("text/plain;charSet=gbk");
		String str=request.getParameter("number");
		byte[] bytes=str.getBytes("ISO-8859-1");
		String number=new String(bytes,"gbk");		
		System.out.println(number);
		Connection dbConn=null;
		try {
		  Class.forName(driverName).newInstance();
		  dbConn = DriverManager.getConnection(dbURL, userName, userPwd);		  
		  Statement stmt=dbConn.createStatement();
//		  ResultSet result=stmt.executeQuery("select pexm,peid,dwmc from u_per where pezw='"+number+"'");
		  ResultSet result=stmt.executeQuery("select u_per.peid,u_per.pexm,u_per.dwmc,"
		  		+ "u_yksf.ykcp,u_yksf.ykje,u_yksf.momc,u_yksf.yksj,u_yksf.hrq,u_yksf.zffs from u_per left join u_yksf "
		  		+ "on u_per.peid=u_yksf.peid  where pezw='"+number+"' order by u_yksf.ykcp, u_yksf.hrq desc");
		  
		  JsonArray ja=new JsonArray();  
		  int columns=result.getMetaData().getColumnCount();
		  ResultSetMetaData md=result.getMetaData();
		  String ykcpfirst=null;
		  String ykcpsecond=null;
		  while(result.next()){  
			  JsonObject jo=new JsonObject();
			  if(ykcpfirst==null){
				  ykcpfirst=result.getString("ykcp");
			  }else{
				  ykcpsecond=result.getString("ykcp");
			  }
			  if(ykcpfirst.equals(ykcpsecond)){
				  continue;
			  }
			  
			  for (int i = 1; i < columns+1; i++) {
				String name=md.getColumnName(i);
				String typename= md.getColumnTypeName(i);
				System.out.println(name+":"+typename);
				if(typename.contains("int")){
				   jo.addProperty(name, result.getInt(i));
				}else{
					jo.addProperty(name, result.getString(i));
				}
			}
			  ja.add(jo);
			  if(ykcpsecond!=null&&!ykcpfirst.equals(ykcpsecond)){//只取一辆车的最晚到期时间
				  break;
			  }
		  }
		  JsonObject joresult=new JsonObject();
		  joresult.addProperty("code", 1);
		  joresult.add("data", ja);  
		  PrintWriter wrter=response.getWriter();
		  wrter.write(joresult.toString());
		  	  
		}
		catch (Exception e) {
		  e.printStackTrace();
		  JsonObject joresult=new JsonObject();
		  joresult.addProperty("code", 0);
		  joresult.addProperty("message", e.getMessage());	
		  PrintWriter wrter=response.getWriter();
		  wrter.write(joresult.toString());
		}finally{
		try {
		 if(dbConn!=null)
		   dbConn.close();
		} catch (SQLException e) {
		// TODO Auto-generated catch block
		  e.printStackTrace();
		  }
		  }
		}
		
	}




